<html lang="en-US">

<head>
    
<title>爬取汽车之家 二手车产品库 - 花落雨忧</title>

<meta property="og:title" content="爬取汽车之家 二手车产品库 - 花落雨忧">



    



    
    <meta property="description" content="项目地址：https://github.com/go-crawler/car-prices
[&amp;hellip;] 最近经常有人在耳边提起汽车之家，也好奇二手车在国内的价格是怎么样的，因此本次的目标站点是 汽车之家 的二手车产品库
分析目标源：
[&amp;hellip;] 爬取步骤
[&amp;hellip;] 通过页面查看，可发现在城市筛选区可得到全部的二手车城市列表，但是你仔细查阅代码。会发现它是 JS 加载 &amp;hellip;">
    <meta property="og:description" content="项目地址：https://github.com/go-crawler/car-prices
[&amp;hellip;] 最近经常有人在耳边提起汽车之家，也好奇二手车在国内的价格是怎么样的，因此本次的目标站点是 汽车之家 的二手车产品库
分析目标源：
[&amp;hellip;] 爬取步骤
[&amp;hellip;] 通过页面查看，可发现在城市筛选区可得到全部的二手车城市列表，但是你仔细查阅代码。会发现它是 JS 加载 &amp;hellip;">
    






<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1">

<link rel="shortcut icon" href="https://www.lican.site/logo/logo.png" type="image/x-icon" />



<link rel="stylesheet" href="/css/style.min.css" />

<link rel="stylesheet" href="/css/reset.min.css" />




<script src="https://www.lican.site/js/highlight.min.js"></script>

<script>
hljs.configure({ ignoreUnescapedHTML: true })
hljs.highlightAll();
</script>


<script src="https://www.lican.site/js/jquery.min.js"></script>




<link href="https://www.lican.site/css/hugo-code.min.css" rel="stylesheet" />



    <style>
        .post-content img {
            max-width: 400px;
        }
    </style>
</head>

<body id="period" class="home blog">
    <a class="skip-content" href="#main">Press "Enter" to skip to content</a>
    <div id="overflow-container" class="overflow-container">
        <header class="site-header" id="site-header" role="banner">
    <div class="max-width">
        <div id="title-container" class="title-container">
            <div id="site-title" class="site-title"><a href="/">花落雨忧</a></div>
            <p class="tagline">思所及 力所行 方为真.</p>
        </div>
        
        <div id="menu-primary-container" class="menu-primary-container">
            <div class="icon-container">
            </div>
            <div id="menu-primary" class="menu-container menu-primary" role="navigation">
                <nav class="menu">
                    <ul id="menu-primary-items" class="menu-primary-items">
                        
                        
                        <li id="menu-item-0"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/posts" aria-current="page" tabindex="0">首页</a></li>
                        
                        <li id="menu-item-1"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/tech/" aria-current="page" tabindex="1">技术文档</a></li>
                        
                        <li id="menu-item-2"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/article/" aria-current="page" tabindex="2">文章</a></li>
                        
                        <li id="menu-item-3"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/project/" aria-current="page" tabindex="3">项目</a></li>
                        
                        <li id="menu-item-4"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/about/" aria-current="page" tabindex="4">关于</a></li>
                        
                    </ul>
                </nav>
            </div>
        </div>
    </div>
</header>
        <div id="primary-container" class="primary-container">
            <div class="max-width">
                <section id="main" class="main" role="main">
                    <div id="loop-container" class="loop-container">
                        <div
                            class="post type-post status-publish format-standard hentry entry">
                            <article>
                                <div class="post-container">
                                    <div class="post-header">
                                        <h2 class="post-title">
                                            <a href="/posts/go/crawler/2018-04-01-cars/">爬取汽车之家 二手车产品库</a>
                                        </h2>
                                        
                                        <div class="post-byline">Published on
                                            <a class="date" href="javascript:;">2018/04/01</a>
                                            
                                            
                                            
                                            By 
                                            <a class="author" href="javascript:;">煎鱼</a></div>
                                            
                                        
                                    </div>
                                    <div class="post-content">
                                        <p>项目地址：https://github.com/go-crawler/car-prices</p>
<h2 id="目标">目标</h2>
<p>最近经常有人在耳边提起汽车之家，也好奇二手车在国内的价格是怎么样的，因此本次的目标站点是 <a href="https://car.autohome.com.cn/2sc/440399/index.html">汽车之家</a> 的二手车产品库</p>
<p><img src="https://i.loli.net/2018/03/30/5abe47f82a01f.png" alt="image"></p>
<p>分析目标源：</p>
<ul>
<li>一页共 24 条</li>
<li>含分页，但这个老产品库，在 100 页后会存在问题，因此我们爬取 99 页</li>
<li>可以获取全部城市</li>
<li>共可爬取 19w+ 数据</li>
</ul>
<h2 id="开始">开始</h2>
<p>爬取步骤</p>
<ul>
<li>获取全部的城市</li>
<li>拼装全部城市 URL 入队列</li>
<li>解析二手车页面结构</li>
<li>下一页 URL 入队列</li>
<li>循环拉取所有分页的二手车数据</li>
<li>循环拉取队列中城市的二手车数据</li>
<li>等待，确定队列中无新的 URL</li>
<li>爬取的二手车数据入库</li>
</ul>
<h3 id="获取城市">获取城市</h3>
<p><img src="https://i.loli.net/2018/03/31/5abeff11ef583.png" alt="image"></p>
<p>通过页面查看，可发现在城市筛选区可得到全部的二手车城市列表，但是你仔细查阅代码。会发现它是 JS 加载进来的，城市也统一放在了一个变量中</p>
<p><img src="https://i.loli.net/2018/03/31/5abf056389cf0.png" alt="image"></p>
<p>有两种提取方法</p>
<ul>
<li>分析 JS 变量，提取出来</li>
<li>直接将 <code>areaJson</code> 复制出来作为变量解析</li>
</ul>
<p>在这里我们直接将其复制粘贴出来即可，因为这是比较少变动的值</p>
<h3 id="获取分页">获取分页</h3>
<p><img src="https://i.loli.net/2018/03/31/5abf08ec812e2.png" alt="image"></p>
<p>通过分析页面可以得知分页链接是有一定规律的，例如：<code>/2sc/hangzhou/a0_0msdgscncgpi1ltocsp2exb4/</code>，可以发现 <code>sp%d</code>，<code>sp</code> 后面为页码</p>
<p>按照常理，可以通过预测所有分页链接，推入队列后 <code>go routine</code> 一波 即可快速拉取</p>
<p>但是在这老产品库存在一个问题，在超过 100 页后，下一页永远是 101 页</p>
<p><img src="https://i.loli.net/2018/03/31/5abf0e1e623ec.png" alt="image"></p>
<p>因此我们采取比较传统的做法，通过拉取下一页的链接去访问，以便适应可能的分页链接改变； 100 页以后的分页展示也很奇怪，先忽视</p>
<h3 id="获取二手车数据">获取二手车数据</h3>
<p>页面结构较为固定，常规的清洗 HTML 即可</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">GetCars</span>(<span style="color:#a6e22e">doc</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">goquery</span>.<span style="color:#a6e22e">Document</span>) (<span style="color:#a6e22e">cars</span> []<span style="color:#a6e22e">QcCar</span>) {
	<span style="color:#a6e22e">cityName</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">GetCityName</span>(<span style="color:#a6e22e">doc</span>)
	<span style="color:#a6e22e">doc</span>.<span style="color:#a6e22e">Find</span>(<span style="color:#e6db74">&#34;.piclist ul li:not(.line)&#34;</span>).<span style="color:#a6e22e">Each</span>(<span style="color:#66d9ef">func</span>(<span style="color:#a6e22e">i</span> <span style="color:#66d9ef">int</span>, <span style="color:#a6e22e">selection</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">goquery</span>.<span style="color:#a6e22e">Selection</span>) {
		<span style="color:#a6e22e">title</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">selection</span>.<span style="color:#a6e22e">Find</span>(<span style="color:#e6db74">&#34;.title a&#34;</span>).<span style="color:#a6e22e">Text</span>()
		<span style="color:#a6e22e">price</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">selection</span>.<span style="color:#a6e22e">Find</span>(<span style="color:#e6db74">&#34;.detail .detail-r&#34;</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#e6db74">&#34;.colf8&#34;</span>).<span style="color:#a6e22e">Text</span>()
		<span style="color:#a6e22e">kilometer</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">selection</span>.<span style="color:#a6e22e">Find</span>(<span style="color:#e6db74">&#34;.detail .detail-l&#34;</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#e6db74">&#34;p&#34;</span>).<span style="color:#a6e22e">Eq</span>(<span style="color:#ae81ff">0</span>).<span style="color:#a6e22e">Text</span>()
		<span style="color:#a6e22e">year</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">selection</span>.<span style="color:#a6e22e">Find</span>(<span style="color:#e6db74">&#34;.detail .detail-l&#34;</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#e6db74">&#34;p&#34;</span>).<span style="color:#a6e22e">Eq</span>(<span style="color:#ae81ff">1</span>).<span style="color:#a6e22e">Text</span>()

		<span style="color:#a6e22e">kilometer</span> = <span style="color:#a6e22e">strings</span>.<span style="color:#a6e22e">Join</span>(<span style="color:#a6e22e">compileNumber</span>.<span style="color:#a6e22e">FindAllString</span>(<span style="color:#a6e22e">kilometer</span>, <span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>), <span style="color:#e6db74">&#34;&#34;</span>)
		<span style="color:#a6e22e">year</span> = <span style="color:#a6e22e">strings</span>.<span style="color:#a6e22e">Join</span>(<span style="color:#a6e22e">compileNumber</span>.<span style="color:#a6e22e">FindAllString</span>(<span style="color:#a6e22e">strings</span>.<span style="color:#a6e22e">TrimSpace</span>(<span style="color:#a6e22e">year</span>), <span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>), <span style="color:#e6db74">&#34;&#34;</span>)
		<span style="color:#a6e22e">priceS</span>, <span style="color:#a6e22e">_</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">strconv</span>.<span style="color:#a6e22e">ParseFloat</span>(<span style="color:#a6e22e">price</span>, <span style="color:#ae81ff">64</span>)
		<span style="color:#a6e22e">kilometerS</span>, <span style="color:#a6e22e">_</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">strconv</span>.<span style="color:#a6e22e">ParseFloat</span>(<span style="color:#a6e22e">kilometer</span>, <span style="color:#ae81ff">64</span>)
		<span style="color:#a6e22e">yearS</span>, <span style="color:#a6e22e">_</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">strconv</span>.<span style="color:#a6e22e">Atoi</span>(<span style="color:#a6e22e">year</span>)

		<span style="color:#a6e22e">cars</span> = append(<span style="color:#a6e22e">cars</span>, <span style="color:#a6e22e">QcCar</span>{
			<span style="color:#a6e22e">CityName</span>: <span style="color:#a6e22e">cityName</span>,
			<span style="color:#a6e22e">Title</span>: <span style="color:#a6e22e">title</span>,
			<span style="color:#a6e22e">Price</span>: <span style="color:#a6e22e">priceS</span>,
			<span style="color:#a6e22e">Kilometer</span>: <span style="color:#a6e22e">kilometerS</span>,
			<span style="color:#a6e22e">Year</span>: <span style="color:#a6e22e">yearS</span>,
		})
	})

	<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">cars</span>
}
</code></pre></div><h2 id="数据">数据</h2>
<p><img src="https://i.loli.net/2018/03/31/5abf1d8042196.png" alt="image"></p>
<p><img src="https://i.loli.net/2018/04/01/5abfbaa14b09c.png" alt="image"></p>
<p>在各城市的平均价格对比中，我们可以发现北上广深里的北京、上海、深圳都在榜单上，而近年势头较猛的杭州直接占领了榜首，且后几名都有一些距离</p>
<p>而其他城市大致都是梯级下降的趋势，看来一线城市的二手车也是不便宜了，当然这只是均价</p>
<p><img src="https://i.loli.net/2018/03/31/5abf1dbc665f2.png" alt="image"></p>
<p>我们可以看到价格和公里数的对比，上海、成都、郑州的等比差异是有点大，感觉有需求的话可以在价格和公里数上做一个衡量</p>
<p><img src="https://i.loli.net/2018/03/31/5abf1e1434edc.png" alt="image"></p>
<p>这图有点儿有趣，粗略的统计了一下总公里数。在前几张图里，平均价格排名较高的统统没有出现在这里，反倒是呼和浩特、大庆、中山等出现在了榜首</p>
<p>是否侧面反应了一线城市的车辆更新换代较快，而较后的城市的车辆倒是换代较慢，公里数基本都杠杠的</p>
<p><img src="https://i.loli.net/2018/03/31/5abf1e4936640.png" alt="image"></p>
<p>通过对标题的分析，可以得知车辆产品库的命名基本都是品牌名称+自动/手动+XXXX 款+属性，看标题就能知道个概况了</p>
<h2 id="参考">参考</h2>
<h3 id="爬虫项目地址">爬虫项目地址</h3>
<ul>
<li><a href="https://github.com/go-crawler/car-prices">https://github.com/go-crawler/car-prices</a></li>
</ul>


                                        
                                        
                                        
                                        <div class="rp4wp-related-posts">
                                            <h3>相关文章</h3>
                                            <ul>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.site/posts/go/crawler/2018-03-21-douban-top250/">爬取豆瓣电影 Top250</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.site/posts/go/gin/2018-03-26-cgo/">「番外」Golang 交叉编译</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.site/posts/go/gin/2018-03-24-golang-docker/">「连载九」将Golang应用部署到Docker</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.site/posts/go/gin/2018-03-18-swagger/">「连载八」为它加上Swagger</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.site/posts/go/gin/2018-03-15-reload-http/">「连载七」优雅的重启服务</a>
                                                    </div>
                                                </li>
                                                
                                            </ul>
                                        </div>
                                        
                                        
                                    </div>

                                    
                                    
                                    

                                    
                                    <div class="post-meta">
                                        
                                        
                                        <div class="post-tags">
                                            <ul>
                                            
                                            <li>
                                                <a href="/tags/go" title="View all posts tagged match">go</a>
                                            </li>
                                            
                                            <li>
                                                <a href="/tags/%e6%95%b0%e6%8d%ae%e5%88%86%e6%9e%90" title="View all posts tagged match">数据分析</a>
                                            </li>
                                            
                                            </ul>
                                        </div>
                                        
                                        
                                        <nav class="further-reading">
                                            
                                            <div class="previous">
                                                <span>&lt;&lt; Prev</span>
                                                <a href="https://www.lican.site/posts/go/gin/2018-03-26-cgo/"
                                                    rel="prev">「番外」Golang 交叉编译</a> </div>
                                            
                                            
                                            <div class="next">
                                                <span>Next >></span>
                                                <a href="https://www.lican.site/posts/go/gin/2018-04-15-gorm-callback/">「连载十」定制 GORM Callbacks</a> 
                                            </div>
                                            
                                        </nav>
                                    </div>
                                    
                                    

                                    
                                    
                                    

                                </div>
                            </article>
                        </div>
                    </div>
                </section>
                <aside class="sidebar sidebar-primary" id="sidebar-primary" role="complementary">
    <h1 class="screen-reader-text">Sidebar</h1>
    

    
    
    <section id="text-2" class="widget widget_text">
        <div class="textwidget">
            
            <div id="profile">
                <div id="profile_picture"><img src="https://www.lican.site/logo/logo.png"></div>
                <div id="profile_intro">
                    <p><span class="name">Lican</span></p>
                    <p class="intro">全栈开发者，爱好造轮子。</p>
                </div>
            </div>
            
            <p>
                <script type="text/javascript">
                    (function ($) {
                        $(document).ready(function () {
                            var menuPrimaryContainer = $('#menu-primary-container');
                            var profile = $('#text-2');
                            $('#toggle-navigation').click(function () {
                                if (menuPrimaryContainer.hasClass('open')) {
                                    profile.removeClass('open');
                                } else {
                                    profile.addClass('open');
                                }
                            });
                        });
                    })(jQuery);
                </script>
            </p>
        </div>
    </section>
    
    
    
    
    
    <section id="text-5" class="widget widget_text">
        <h2 class="widget-title">开源项目</h2>
        <div class="textwidget">
            <div id="projects" style="line-height: 22px;">
                
                <a href="https://github.com/idoubi/gonews"
                    target="_blank">gonews</a>: &nbsp;Daily news for golang<br>
                
                <a href="https://github.com/idoubi/sql2struct"
                    target="_blank">sql2struct</a>: &nbsp;Generate go struct according to SQL<br>
                
                <a href="https://github.com/idoubi/goz"
                    target="_blank">goz</a>: &nbsp;Request library used in golang<br>
                
        </div>
    </section>
    
    

    
    
    
    
    

    
    
    

    
    
    

    
    
    
    
</aside>
            </div>
        </div>

        <footer id="site-footer" class="site-footer" role="contentinfo">
    <div class="max-width">
    </div>
    <div class="footer">
        <div id="footercontent">
            © lican.vip All rights reserved<br/>
            Built with Hugo Theme <a href="https://github.com/idoubi/hugo-theme-period" target="_blank">Period</a>
        </div>
    </div>
</footer>

<script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?e8351b6d4626d5881d439ea1f6184baa";
      var s = document.getElementsByTagName("script")[0]; 
      s.parentNode.insertBefore(hm, s);
    })();
</script>
    
    
    </div>
    
</body>

</html>